package yu.liu.personal.app.myapplication.utils;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import java.io.InputStream;

import yu.liu.personal.app.myapplication.models.AVGList;

/**
 * Created by Administrator on 2016/9/25.
 */

public class DataReader {

    private static String jsonUri_chs = "data_json_chs.json";

    private static String jsonUri_jp = "data_json_jp.json";

    public final static int AREA_CODE_CHS = 0;

    public final static int AREA_CODE_CHT = 1;

    public static AVGList getListFromAssets(Context context, int areaCode) {
        AVGList list = null;
        try {
            switch (areaCode) {
                case AREA_CODE_CHS:
                    InputStream inStream = context.getResources().getAssets()
                            .open(jsonUri_chs);
                    int length = inStream.available();
                    byte[] buffer = new byte[length];
                    int count = inStream.read(buffer);
                    if(count == length) {
                        String result = new String(buffer);
                        Log.i("json", result);
                        list = JSONObject.parseObject(result, AVGList.class);
                    } else {
                        Log.e("JSON_ERR", "Data reader internal error!");
                    }
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
